#!/usr/bin/env python
#-*- coding:utf-8 -*-
import os,sys
import shutil
from wwpy.io.Source import Source

class File(Source):
    
    def __init__(self,file):
        super(File,self).__init__(file)
        self.file=file
    
    @property
    def size(self):
        if File.is_file(self.file):
            return os.path.getsize(self.file)
        return 0
    
    @staticmethod
    def format_size(size,mode='M'):
        size_dict={'B':1.0,'K':1024.0,'M':1024*1024.0,'G':1024*1024*1024.0,'T':1024*1024*1024*1024.0,'P':1024*1024*1024*1024*1024.0}
        if mode in size_dict:
            return "%0.1f" % (size/size_dict[mode])
        return 0
    
    @staticmethod
    def readable(size):
        units="B,KB,MB,GB,TB,PB".split(',')
        unit=0
        while size>1024:
            size/=1024
            unit+=1
        return "%.1f%s" % (size,units[unit])
    
    def stat(self):
        return os.stat(self.file)
    
    @staticmethod
    def copy(from_,to_):
        if File.is_dir(from_):
            shutil.copy(from_,to_)
        else:
            shutil.copyfile(from_,to_)
        
    @staticmethod
    def move(from_,to_):
        shutil.move(from_,to_)
        
    def rename(self,new):
        os.rename(File.file,new)
        
    def chmod(self,mode=777):
        os.chmod(self.file,mode)
        
    @staticmethod
    def mkdir(path,mode=777):
        if not File.exists(path):
            os.makedirs(path,mode=mode)
        
    @staticmethod
    def exists(path):
        return os.path.exists(path)
        
    @staticmethod
    def remove(path):
        if File.is_dir(path):
            shutil.rmtree(path)
        elif File.is_file(path):
            os.remove(path)
            
    @staticmethod
    def chdir(path):
        os.chdir(path)
        
    @staticmethod
    def cwd(self):
        return os.getcwd()
        
    @property
    def dir_name(self):
        path=os.path.dirname(self.file)
        if path is None or path=='':
            return '.'
        return path
        
    @property
    def base_name(self):
        return os.path.basename(self.file)
    
    @property
    def main_name(self):
        parts=self.base_name.split('.')
        if len(parts)>1:
            return ''.join(parts[:-1])
        return ''.join(parts)
    
    @property
    def ext_name(self):
        parts=self.base_name.split('.')
        if len(parts)>1:
            return parts[-1]
        return None
    
    @staticmethod
    def is_file(path):
        return os.path.isfile(path)
        
    @staticmethod
    def is_dir(path):
        return os.path.isdir(path)
        
    @staticmethod
    def is_abs(path):
        return os.path.isabs(path)
    
    @staticmethod
    def list_dir(path=None,has_path=True,grep=None):
        if path is None:
            path=self.get_cwd()
        if not File.is_dir(path):
            print path+" is not a path!"
            return []
        files=os.listdir(path)
        if grep is not None:
            regex=r
        sorted_files=[]
        for file in files:
            if file=='.' or file=='..':
                continue
            
            if has_path:
                path_file=path+os.sep+file
            else:
                path_file=file
            sorted_files.append(path_file)
        return sorted(sorted_files)
    
    @staticmethod
    def scp(file,user='root',host=None,port=22,path=None):
        scp_cmd="scp -r %s %s@%s:%s" % (file,user,host,path)
        return os.system(scp_cmd)
        
    @staticmethod
    def rsync(file,user='root',host=None,port=None,path=None):
        rsync_cmd="rsync -avz %s %s@%s:%s" % (file,user,host,path)
        return os.system(rsync)
    
    def wc(self):
        s=open(self.file).read()
        return map(len,[s]+map(s.split,(None,'\n','\n\n')))
    
